import axios from 'axios';
import { base64encode } from './base64encode'
// let webToken = '0';

/* ajax-get */
export const fetch = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		axios({
			method: 'GET',
			url: url,
			params: data,
			dataType: 'json',
		}).then(resp => {
			resolve(resp)
		}, reject)
	})
};

/* ajax-get webToken */
export const fetchData = (url, data = {}, webToken) => {
	return new Promise((resolve, reject) => {
		axios({
			method: 'GET',
			url: url,
			dataType: 'json',
			headers: {
				"Authorization": 'Basic ' + base64encode('user:123456'),
				"webToken": webToken
			},
			params: data
		}).then(resp => {
			resolve(resp)
		}, reject)
	})
};

/* json-post */
export const postJsonToken = (url, data = {}, webToken) => {
	return new Promise((resolve, reject) => {
		axios({
			method: 'POST',
			url: url,
			dataType: 'json',
			headers: {
				"Authorization": 'Basic ' + base64encode('user:123456'),
				"webToken": webToken,
				"version": '3.0.0'
			},
			params: data
		}).then(resp => {
			resolve(resp)
		}, reject)
	})
};

/* ajax-post */
export const postJson = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		axios({
			method: 'POST',
			url: url,
			headers: {
				"Authorization": 'Basic ' + base64encode('user:123456'),
				"version": '3.0.0'
			},
			params: data,
		}).then(resp => {
			resolve(resp)
		}, reject)
	})
};
